# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class GroupController < ApplicationController

  # Define default Main Tab Name
    $CURRENT_MAIN_TAB_NAME = "Dashboard"
    $CURRENT_SUB_TAB_NAME = "User List"
    
    def not_enterprise_admin
    if current_user.is_enterprise =='y'
      return false
    else
      flash[:notice] = "You do not have enough privileges to access this action. You need[enterprise admin role]." 
      return true
    end    
  end
  
  def search 
       @name = params[:name]
      
    
       @query = 'SELECT * FROM groups ';
       @whereclause = '';
       if(@name != '')
          if(@whereclause=='')
                 @whereclause = @whereclause +" WHERE name ='" + @name + "'"; 
          else
                @whereclause = @whereclause +" AND name ='" + @name + "'"; 
           end
       end 
       
              
       @query=@query + @whereclause;
       @groups = Group.find_by_sql [@query];
              
   end
 #end of search functionality
  
  
  
  def edit
    if not_enterprise_admin
      redirect_to :controller => "/user", :action => "home"
    end
    @group = Group.find(params['id'])
  end
  
   def list
    if not_enterprise_admin
      redirect_to :controller => "/user", :action => "home"
    end
    
   @groups = Group.find_all 
   
   if params[:name] == nil 
         # do nothing 
   else 
    @name = params[:name]
      
    
       @query = 'SELECT * FROM groups ';
       @whereclause = '';
       if(@name != '')
          if(@whereclause=='')
                 @whereclause = @whereclause +" WHERE name ='" + @name + "'"; 
          else
                @whereclause = @whereclause +" AND name ='" + @name + "'"; 
           end
       end 
             
       @query=@query + @whereclause;
       @groups = Group.find_by_sql [@query];
    end   
    
    # [paginate :organization, :per_page => 10] 
    @user  =  current_user
    #print "User company id=>"=>@user.company_id
  end
  
  def update
    if not_enterprise_admin
      redirect_to :controller => "/user", :action => "home"
    end

    print  @params['groupId']
    group = Group.find(params['groupId'])
    group.name = params['groupName']
  
     if group.save
         flash[:simplemsg] = "Group info saved." 
         redirect_to :controller => "/group", :action => "list"
    end
   end 
  
  
  
  
end
